package com.probiz.estore.content.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.probiz.estore.common.model.catalog.ContentCategory;
import com.probiz.estore.common.model.content.Content;
import com.probiz.estore.common.model.content.RelatedContent;
import com.probiz.estore.content.dao.RelatedContentDao;
import com.probiz.estore.content.model.ContentConstants;
import com.probiz.estore.content.service.RelatedContentManager;
import com.probiz.estore.core.service.impl.GenericManagerImpl;


/**
 * Manager implementation for RelatedContent, responsible for business processing, and communicate between web and persistence layer.
 */
public class RelatedContentManagerImpl extends GenericManagerImpl<RelatedContent> implements RelatedContentManager {

	private RelatedContentDao relatedContentDao = null;

	/**
	 * @param relatedContentDao
	 *            the relatedContentDao to set
	 */
	public void setRelatedContentDao(RelatedContentDao relatedContentDao) {
		this.relatedContentDao = relatedContentDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = relatedContentDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(RelatedContent entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(RelatedContent entity) {

	}

	
	public List<RelatedContent> getRelatedContentsByRecommendedTypeIdBySourceId(
			int contentRelatedTypeId, Integer sourceKind, Integer sourceId,
			int firstResult, int maxResults) {
		return relatedContentDao.getRelatedContentsByRecommendedTypeIdBySourceId(contentRelatedTypeId, sourceKind, sourceId, firstResult, maxResults);
	}
	
	public RelatedContent getRelatedContent(Integer recommendedTypeId,
			int sourceKind, Integer sourceId, Integer contentId) {
		return relatedContentDao.getRelatedContent(recommendedTypeId, sourceKind, sourceId, contentId);
	}
	
	public List<Content> getContentsByRelatedTypeNameBySourceId(
			String contentRelatedTypeName, int sourceKind, Integer sourceId,
			int firstResult, int maxResults) {
		return relatedContentDao.getContentsByRelatedTypeNameBySourceId(contentRelatedTypeName, sourceKind, sourceId, firstResult, maxResults);
	}
	
	public List<Content> getContentsByRelatedTypeIdBySourceId(
			Integer contentRelatedTypeId, int sourceKind, Integer sourceId,
			int firstResult, int maxResults) {
		return relatedContentDao.getContentsByRelatedTypeIdBySourceId(contentRelatedTypeId, sourceKind, sourceId, firstResult, maxResults);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.service.RelatedContentManager#saveRelatedContentsFromArrays(com.probiz.estore.common.model.content.Content, com.probiz.estore.common.model.catalog.ContentCategory, java.lang.String[], java.lang.String[])
	 */
	public void saveRelatedContentsFromArrays(Content sourceContent,ContentCategory sourceCategory,String[] recommendedTypeIds,String[] contentIds){
		List<RelatedContent> formerRcList = new ArrayList<RelatedContent>(); //必须用这种方法 ArrayList支持remove方法
		int sourceKind;
		Integer sourceId;
		if(null != sourceContent){
			//原来的推荐文章
			for(RelatedContent formerRc : sourceContent.getRelatedContents()){
				formerRcList.add(formerRc);
			}
			sourceKind = ContentConstants.RELATED_SOURCE_KIND_CONTENT;
			sourceId = sourceContent.getContentId();
		}else{
			//原来的推荐文章
			for(RelatedContent formerRc : sourceCategory.getRelatedContents()){
				formerRcList.add(formerRc);
			}
			sourceKind = ContentConstants.RELATED_SOURCE_KIND_CONTENTCATEGORY;
			sourceId = sourceCategory.getCategoryId();
		}	
		
		
		
		for(int i=0; i< recommendedTypeIds.length; i++){
			RelatedContent rc = relatedContentDao.getRelatedContent(Integer.parseInt(recommendedTypeIds[i]), sourceKind, sourceId, Integer.parseInt(contentIds[i]));
			if(null != rc){
				//修改
				rc.setSortOrder(i);
				if(formerRcList.contains(rc)){
					formerRcList.remove(rc);
				}
			}else{
				//新增
				rc = new RelatedContent();
				rc.setContentRelatedTypeId(Integer.parseInt(recommendedTypeIds[i]));
				rc.setSourceContent(sourceContent);
				rc.setSourceCategory(sourceCategory);
				rc.setContentId(Integer.parseInt(contentIds[i]));
				rc.setSortOrder(i);
				rc.setSourceKind(Short.valueOf((short)sourceKind));
			}
			relatedContentDao.save(rc);
		}	
		
		//删除
		for(RelatedContent rc: formerRcList){
			relatedContentDao.delete(rc);
		}
	}


}
